package com.example.testlib

/**
 * Created by wenjc@rd.netease.com
 * Date: 2021/6/7
 */

/**
 * 简单判断是否英文语句（字母占比超过一半）
 */
fun CharSequence.isEnSent(): Boolean {
    val half = length / 2
    var charNum = 0
    for (i in 0 until length) {
        val c = this[i]
        if (c in 'a'..'z' || c in 'A'..'Z') {
            charNum++
            //快速判断，读取超过一半且字母占比一半，直接返回true
            if (charNum >= half) {
                return true
            }
        }
        //快速判断，读取超过一半且字母为0，直接返回false
        if (charNum == 0 && i > half) {
            return false
        }
    }
    //字母占比超过一半
    return charNum > half
}